package com.yiqi.liebang.feature.mine.view;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseFragment;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.UserCardBo;
import com.yiqi.liebang.entity.bo.UserCenterBo;
import com.yiqi.liebang.entity.bo.UserDataBo;
import com.yiqi.liebang.feature.login.view.LoginActivity;
import com.yiqi.liebang.feature.mine.contract.UserContract;
import com.yiqi.liebang.feature.mine.dependencies.login.DaggerUserComponent;
import com.yiqi.liebang.feature.mine.dependencies.login.UserPresenterModule;
import com.yiqi.liebang.feature.order.view.OrderActivity;
import com.yiqi.liebang.feature.setting.view.SettingActivity;
import com.yiqi.liebang.feature.wallet.view.WalletActivity;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment implements UserContract.View {


    @Inject
    UserContract.Presenter mPresenter;
    @BindView(R.id.tv_mine_username)
    TextView mTvMineUsername;
    @BindView(R.id.iv_user_head)
    ImageView mIvUserHead;
    @BindView(R.id.tv_mine_position)
    TextView mTvMinePosition;
    @BindView(R.id.tv_mine_Influence)
    TextView mTvMineInfluence;
    Unbinder unbinder;
    @BindView(R.id.iv_user_position)
    ImageView mIvUserPosition;
    @BindView(R.id.iv_user_basic)
    ImageView mIvUserBasic;
    Unbinder unbinder1;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initInjector() {
        DaggerUserComponent.builder().userPresenterModule(new UserPresenterModule(this)).build().inject(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (AM.user().isLogin()) {
            initUserInfo();
        }else {
            mTvMineUsername.setText("未登录");
            mTvMinePosition.setText("");
            AM.image().bindToCircleObject("", mIvUserHead);
            mTvMineInfluence.setText("");
            mIvUserBasic.setVisibility(View.GONE);
            mIvUserPosition.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initView() {


    }

    @Override
    protected void initData() {
        if (!AM.user().isLogin()) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
        } else {
            initUserInfo();
        }
    }

    private void initUserInfo() {
        if (AM.user().isLogin()) {
            mPresenter.getMineData();
        } else {
            mTvMineUsername.setText("未登录");
            mTvMinePosition.setText("");
            AM.image().bindToCircleObject("", mIvUserHead);
            mTvMineInfluence.setText("");
        }
    }

    @Override
    protected void initEvent() {

    }


    @OnClick({R.id.btn_login, R.id.btn_mine_edit_userinfo, R.id.btn_mine_to_order,
            R.id.btn_mine_to_wallet, R.id.btn_mine_to_coupon, R.id.btn_mine_to_viefty,
            R.id.btn_mine_to_jinangli, R.id.btn_mine_to_seeting})
    public void onViewClicked(View view) {

        if (!AM.user().isLogin()) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
            return;
        }
        switch (view.getId()) {
            case R.id.btn_login:
                Intent intentLogin = new Intent(getActivity(), UserInfoActivity.class);
                intentLogin.putExtra("isEdit", false);
                startActivity(intentLogin);
                break;
            case R.id.btn_mine_edit_userinfo:
                Intent intentUser = new Intent(getActivity(), UserInfoActivity.class);
                intentUser.putExtra("isEdit", true);
                startActivity(intentUser);
                break;
            case R.id.btn_mine_to_order:
                startActivity(new Intent(getActivity(), OrderActivity.class));
                break;
            case R.id.btn_mine_to_wallet:
                startActivity(new Intent(getActivity(), WalletActivity.class));
                break;
            case R.id.btn_mine_to_coupon:
                Intent intent = new Intent(getActivity(), CouponActivity.class);
                intent.putExtra("isShow", true);
                startActivity(intent);
                break;
            case R.id.btn_mine_to_viefty:
                startActivity(new Intent(getActivity(), ToExpertActivity.class));
                break;
            case R.id.btn_mine_to_jinangli:

                startActivity(new Intent(getActivity(), InfluenceRewardActivity.class));

                break;
            case R.id.btn_mine_to_seeting:
                Intent intents = new Intent(getActivity(), SettingActivity.class);
                startActivity(intents);
                break;
        }
    }


    @Override
    public void showUserCenterSuccess(UserCenterBo s) {

    }

    @Override
    public void showUserCenterError(String error) {

    }

    @Override
    public void showUserCardSuccess(UserCardBo s) {

    }

    @Override
    public void showMineSuccess(UserDataBo s) {
        RongIM.getInstance().setCurrentUserInfo(new UserInfo(s.getUserUid(), s.getUserName(),
                TextUtils.isEmpty(s.getUserHead()) ? Uri.parse("") : Uri.parse(s.getUserHead())));
        mTvMineUsername.setText(s.getUserName());

        String content = TextUtils.isEmpty(s.getPosition())?"":" · "+s.getPosition();
        mTvMinePosition.setText(Html.fromHtml(content));
        AM.image().bindToCircleObject(s.getUserHead(), mIvUserHead);
        mTvMineInfluence.setText(Html.fromHtml("影响力 <font color=\"#00ADEF\">" + s.getEffectScore() + "</font>"));
        mIvUserBasic   .setVisibility(s.getIsBasic() == 1 ? View.VISIBLE : View.GONE);
        mIvUserPosition.setVisibility(s.getIsOccupation() == 1 ? View.VISIBLE : View.GONE);
        RongIM.getInstance().setCurrentUserInfo(new UserInfo(s.getUserUid(), s.getUserName(),
                TextUtils.isEmpty(s.getUserHead()) ? Uri.parse("") : Uri.parse(s.getUserHead())));
        RongIM.getInstance().setMessageAttachedUserInfo(true);
    }

    @Override
    public void showLikeSuccess() {

    }

    @Override
    public void showPrivateSuccess(Boolean error) {

    }

    @Override
    public void showUserEditSuccess() {

    }

    @Override
    public void showDeleteFriendSuccess() {

    }

    @Override
    public void showAddBlackSuccess() {

    }

    @Override
    public void showError(String error) {

    }

}
